<template>
    <div class="container">
        <div class="row">
            <div class="col-3">
                <div class="card" style="margin-top: 20px;"><!--上边距-->
                    <div class="card-body">
                        <img :src="$store.state.user.photo" alt="" style="width: 100%;"><!--填满-->
                    </div><!--有表达式需要加冒号，获取全局信息中的图片url-->
                </div>
            </div>
            <div class="col-9">
                <div class="card" style="margin-top: 20px;"><!--卡片整体，设上边距-->
                    <div class="card-header"><!--卡片头部信息-->
                        <span style="font-size: 130%;">我的Bot</span><!--加大字体-->
                        <button type="button" class="btn btn-primary float-end" data-bs-toggle="modal" data-bs-target="#add-bot-btn">
                            创建Bot<!--float-end表示按钮向右对齐-->
                        </button>
                        
                        <!-- 创建bot的Modal弹窗在中间 -->
                        <div class="modal fade" id="add-bot-btn" tabindex="-1" >
                            <div class="modal-dialog modal-xl">
                            <div class="modal-content">
                                <div class="modal-header">
                                <h1 class="modal-title fs-5" id="exampleModalLabel">创建Bot</h1>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                </div>
                                <div class="modal-body">
                                    <!--表单在中间-->
                                    <div class="mb-3">
                                        <label for="add-bot-title" class="form-label">名称</label>
                                        <input v-model="botadd.title" type="text" class="form-control" id="add-bot-title" placeholder="请输入Bot名称">
                                    </div><!--绑定botadd对象，好和后端交互-->
                                    <div class="mb-3">
                                        <label for="add-bot-description" class="form-label">简介</label>
                                        <textarea v-model="botadd.description" class="form-control" id="add-bot-description" rows="3" placeholder="请输入Bot简介"></textarea>
                                    </div><!--绑定botadd对象，好和后端交互-->
                                    <div class="mb-3">
                                        <label for="add-bot-code" class="form-label">代码</label>
                                        <VAceEditor
                                                        v-model:value="botadd.content"
                                                        @init="editorInit"
                                                        lang="c_cpp"
                                                        theme="textmate"
                                                        style="height: 300px" />
                                    </div><!--代码行数较多，为7行--><!--绑定botadd对象，好和后端交互-->
                                    <!--表单在中间-->
                                </div>
                                <div class="modal-footer">
                                    <div class="error-message">{{botadd.error_message}}</div><!--显示报错信息-->
                                    <button type="button" class="btn btn-primary" @click="add_bot">创建</button>
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                </div>
                            </div>
                            </div>
                        </div>
                        <!-- 创建bot的Modal弹窗在中间 -->

                    </div>
                    <div class="card-body"><!--卡片身体信息，展示所有bot-->
                        <table class="table table-hover">
							<thead><!--表头-->
                                <tr>
                                    <th>名称</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody><!--表体-->
                                 <tr v-for="bot in bots" :key="bot.id"><!--用语法遍历所有bot，key每一行都一样-->
                                    <td>{{ bot.title }}</td>
                                    <td>{{ bot.createtime }}</td>
                                    <td>
                                        <button type="button" class="btn btn-secondary" style="margin-right: 10px;" data-bs-toggle="modal" 
                                                :data-bs-target="'#update-bot-modal-'+bot.id">修改</button><!--设置向右的偏移量-->
                                        <button type="button" class="btn btn-danger" @click="remove_bot(bot)">删除</button><!--自动传入要删除的bot对象-->
                                    </td>

                        <!-- 修改bot的Modal弹窗在中间，Modal最好和按钮放在一块，方便调试 -->
                        <!--修改bot记录，每一条记录都有不同的弹窗，所以可以用每条记录的id（每个bot的id）来对应标识，作为标签id-->
                        <div class="modal fade" :id="'update-bot-modal-'+bot.id" tabindex="-1" ><!--有表达式所以要加冒号-->
                            <div class="modal-dialog modal-xl">
                            <div class="modal-content">
                                <div class="modal-header">
                                <h1 class="modal-title fs-5" id="exampleModalLabel">修改Bot</h1>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                </div>
                                <div class="modal-body">
                                    <!--表单在中间-->
                                    <div class="mb-3">
                                        <label for="add-bot-title" class="form-label">名称</label>
                                        <input v-model="bot.title" type="text" class="form-control" id="add-bot-title" placeholder="请输入Bot名称">
                                    </div><!--绑定botadd对象，好和后端交互-->
                                    <div class="mb-3">
                                        <label for="add-bot-description" class="form-label">简介</label>
                                        <textarea v-model="bot.description" class="form-control" id="add-bot-description" rows="3" placeholder="请输入Bot简介"></textarea>
                                    </div><!--绑定botadd对象，好和后端交互-->
                                    <div class="mb-3">
                                        <label for="add-bot-code" class="form-label">代码</label>
                                        <!--代码框-->
                                        <VAceEditor
                                                        v-model:value="bot.content"
                                                        @init="editorInit"
                                                        lang="c_cpp"
                                                        theme="textmate"
                                                        style="height: 300px" />
                                    </div><!--代码行数较多，为7行--><!--绑定botadd对象，好和后端交互-->
                                    <!--表单在中间-->
                                </div>
                                <div class="modal-footer">
                                    <div class="error-message">{{bot.error_message}}</div><!--显示报错信息-->
                                    <button type="button" class="btn btn-primary" @click="update_bot(bot)">保存修改</button>
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                </div>
                            </div>
                            </div>
                        </div>
                        <!-- 修改bot的Modal弹窗在中间 -->
                                </tr>
                            </tbody>
						</table>

                    
                       
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    //reactive用于绑定对象
import { ref, reactive } from 'vue'
import $ from 'jquery'
import { useStore } from 'vuex'
//用于关闭弹窗
import { Modal } from 'bootstrap/dist/js/bootstrap'

//这两个都是用于动态代码编译
import { VAceEditor } from 'vue3-ace-editor';
import ace from 'ace-builds';

export default {
    components: {//这个代码框是个组件，所以要加入components中
        VAceEditor
    },
    setup(){
        const store = useStore();//获取全局变量，来获取token验证合法性
        let bots = ref([]);//存所有bot
        
        //动态代码编译的设置
        ace.config.set(
            "basePath", 
            "https://cdn.jsdelivr.net/npm/ace-builds@" + require('ace-builds').version + "/src-noconflict/")

        //创建被绑定的对象
        const botadd = reactive({
            title: "",
            description: "",
            content: "",
            error_message: "",
        });

        const refresh_bots = () => {//获取用户信息（不改全局变量的请求，都不用写在store里面）
                $.ajax({
                    url: "http://127.0.0.1:3000/user/bot/getlist/",
                    type: "get",
                    headers: {
                        Authorization: "Bearer " + store.state.user.token,
                    },
                    success(resp) {
                        bots.value = resp;//传入所有bot
                    }
                })
            }
        refresh_bots(); //执行函数

        const add_bot = () => {//创建提交按钮的绑定函数
                    botadd.error_message = "";
                    $.ajax({
                        url: "http://127.0.0.1:3000/user/bot/add/",
                        type: "post",
                        data: {
                            title: botadd.title,//因为这是reactive的对象，所以不用加.value
                            description: botadd.description,
                            content: botadd.content,
                        },
                        headers: {
                            Authorization: "Bearer " + store.state.user.token,
                        },
                        success(resp) {
                            if (resp.error_message === "success") {
                                botadd.title = "";
                                botadd.description = "";
                                botadd.content = "";//创建成功后，要把信息清空，防止污染下一次的创建
                                Modal.getInstance("#add-bot-btn").hide();//传入"#"+模块弹窗的id，用来关闭该弹窗
                                refresh_bots();//刷新列表
                            } else {
                                botadd.error_message = resp.error_message;//获取后端的报错信息
                            }
                        }
                    })
                }

                const update_bot = (bot) => {//需要知道修改的是谁，所以要传入bot参数
                    bot.error_message = "";
                    $.ajax({
                        url: "http://127.0.0.1:3000/user/bot/update/",
                        type: "post",
                        data: {
                            bot_id: bot.id,
                            title: bot.title,
                            description: bot.description,
                            content: bot.content,
                        },
                        headers: {
                            Authorization: "Bearer " + store.state.user.token,
                        },
                        success(resp) {
                            if (resp.error_message === "success") {
                                Modal.getInstance('#update-bot-modal-' + bot.id).hide();//每一条记录的修改都要有不一样的标识，
                                //不然不知道更新哪条消息，所以把bot.id加入标签id
                                refresh_bots();//刷新bot显示
                            } else {
                                bot.error_message = resp.error_message;
                            }
                        }
                    })
                }

                const remove_bot = (bot) => {//删除按钮的绑定函数//需要知道删除的是谁，所以要传入bot参数
                    $.ajax({
                        url: "http://127.0.0.1:3000/user/bot/remove/",
                        type: "post",
                        data: {
                            bot_id: bot.id,
                        },
                        headers: {
                            Authorization: "Bearer " + store.state.user.token,
                        },
                        success(resp) {
                            console.log(resp);
                            if (resp.error_message === "success") {
                                refresh_bots();
                            }
                        },

                    })
                }

        //返回bots数组和botadd对象
        return {
            bots,
            botadd,
            add_bot,
            remove_bot,
            update_bot
        }
    }        
}
</script>

<style scoped>
div.error-message{
    color: red;
}
</style>